#include "globalsettingdialog.h"
#include "ui_globalsettingdialog.h"

#include <QColorDialog>
#include <QFontDialog>
#include <QSettings>

GlobalSettingDialog::GlobalSettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GlobalSettingDialog)
{
    ui->setupUi(this);
    finddata();
}

GlobalSettingDialog::~GlobalSettingDialog()
{
    delete ui;

}

void GlobalSettingDialog::finddata()
{
    QSettings settings;

    setColor(ui->lbHighlightingColor, settings.value("HighlightingColor", QColor(0xff, 0xff, 0x99, 0xff)).value<QColor>());
    setColor(ui->lbAddressAreaColor, settings.value("AddressAreaColor", this->palette().alternateBase().color()).value<QColor>());
    setColor(ui->lbSelectionColor, settings.value("SelectionColor", this->palette().highlight().color()).value<QColor>());
    setColor(ui->lbAddressFontColor, settings.value("AddressFontColor", QPalette::WindowText).value<QColor>());
    setColor(ui->lbAsciiAreaColor, settings.value("AsciiAreaColor", this->palette().alternateBase().color()).value<QColor>());
    setColor(ui->lbAsciiFontColor, settings.value("AsciiFontColor", QPalette::WindowText).value<QColor>());
    setColor(ui->lbHexFontColor, settings.value("HexFontColor", QPalette::WindowText).value<QColor>());
#ifdef Q_OS_WIN32
    ui->leWidgetFont->setFont(settings.value("WidgetFont", QFont("Courier", 10)).value<QFont>());
    ui->leWidgetFont_2->setFont(settings.value("ViewFont", QFont("Courier", 10)).value<QFont>());
    ui->leWidgetFont_3->setFont(settings.value("InsightFont", QFont("Courier", 10)).value<QFont>());
#else
    ui->leWidgetFont->setFont(settings.value("WidgetFont", QFont("Monospace", 10)).value<QFont>());
    ui->leWidgetFont_2->setFont(settings.value("ViewFont", QFont("Monospace", 10)).value<QFont>());
    ui->leWidgetFont_3->setFont(settings.value("InsightFont", QFont("Monospace", 10)).value<QFont>());
#endif
}

void GlobalSettingDialog::savadata()
{
    QSettings settings;

    settings.setValue("HighlightingColor", ui->lbHighlightingColor->palette().color(QPalette::Background));
    settings.setValue("AddressAreaColor", ui->lbAddressAreaColor->palette().color(QPalette::Background));
    settings.setValue("SelectionColor", ui->lbSelectionColor->palette().color(QPalette::Background));
    settings.setValue("AddressFontColor", ui->lbAddressFontColor->palette().color(QPalette::Background));
    settings.setValue("AsciiAreaColor", ui->lbAsciiAreaColor->palette().color(QPalette::Background));
    settings.setValue("AsciiFontColor", ui->lbAsciiFontColor->palette().color(QPalette::Background));
    settings.setValue("HexFontColor", ui->lbHexFontColor->palette().color(QPalette::Background));
    settings.setValue("WidgetFont",ui->leWidgetFont->font());
    settings.setValue("ViewFont",ui->leWidgetFont_2->font());
    settings.setValue("InsightFont",ui->leWidgetFont_3->font());
}

void GlobalSettingDialog::setColor(QWidget *widget, QColor color)
{
    QPalette palette = widget->palette();
    palette.setColor(QPalette::Background, color);
    widget->setPalette(palette);
    widget->setAutoFillBackground(true);

}

void GlobalSettingDialog::on_pbHighlightingColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbHighlightingColor->palette().color(QPalette::Background), this);
    if (color.isValid())
        setColor(ui->lbHighlightingColor, color);
}


void GlobalSettingDialog::on_pbWidgetFont_clicked()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, ui->leWidgetFont->font(), this);
    if (ok)
        ui->leWidgetFont->setFont(font);
}


void GlobalSettingDialog::on_pbSelectionColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbSelectionColor->palette().color(QPalette::Background), this);
    if (color.isValid())
        setColor(ui->lbSelectionColor, color);
}


void GlobalSettingDialog::on_pbAddressAreaColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbAddressAreaColor->palette().color(QPalette::Background), this);
    if (color.isValid())
        setColor(ui->lbAddressAreaColor, color);
}


void GlobalSettingDialog::on_pbAddressFontColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbAddressFontColor->palette().color(QPalette::WindowText), this);
    if (color.isValid())
        setColor(ui->lbAddressFontColor, color);
}


void GlobalSettingDialog::on_pbAsciiAreaColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbAsciiAreaColor->palette().color(QPalette::Background), this);
    if (color.isValid())
        setColor(ui->lbAsciiAreaColor, color);
}


void GlobalSettingDialog::on_pbAsciiFontColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbAsciiFontColor->palette().color(QPalette::WindowText), this);
    if (color.isValid())
        setColor(ui->lbAsciiFontColor, color);
}


void GlobalSettingDialog::on_pbHexFontColor_clicked()
{
    QColor color = QColorDialog::getColor(ui->lbHexFontColor->palette().color(QPalette::WindowText), this);
    if (color.isValid())
        setColor(ui->lbHexFontColor, color);
}


void GlobalSettingDialog::on_pbWidgetFont_2_clicked()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, ui->leWidgetFont_2->font(), this);
    if (ok)
        ui->leWidgetFont_2->setFont(font);
}


void GlobalSettingDialog::on_pbWidgetFont_3_clicked()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok, ui->leWidgetFont_3->font(), this);
    if (ok)
        ui->leWidgetFont_3->setFont(font);
}

